package com.clp.protocol.iec104.apdu.asdu.info_obj.qua;

import com.clp.protocol.core.utils.ByteUtil;
import com.clp.protocol.iec104.definition.qua_type.InitCauseQuaType;
import io.netty.buffer.ByteBuf;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Objects;

/**
 * 初始化结束限定词
 */
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class M_EI_NA_1_Qua extends Qua {
    private InitCauseQuaType type;

    @Override
    public M_EI_NA_1_Qua initBy(ByteBuf buf) {
        this.type = InitCauseQuaType.gain(buf.readByte());
        return this;
    }

    @Override
    public boolean isValid() {
        return type != null;
    }

    @Override
    public void writeTo(ByteBuf buf) {
        buf.writeByte(type.getVal());
    }

    @Override
    public String toString() {
        return type.getDesc() + "(" + ByteUtil.byteToHexStr(type.getVal(), true) + ")";
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        M_EI_NA_1_Qua that = (M_EI_NA_1_Qua) o;
        return type == that.type;
    }

    @Override
    public int hashCode() {
        return Objects.hash(type);
    }
}
